package com.taojie.graduation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taojie.graduation.domain.Category;
import com.taojie.graduation.mapper.CategoryMapper;
import com.taojie.graduation.service.api.CategoryService;
import com.taojie.graduation.utils.Result;
import com.taojie.graduation.utils.ResultCode;
import com.taojie.graduation.utils.Tree;
import com.taojie.graduation.utils.TreeUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CategoryServiceImpl
 * @Description TODO
 * @Author Chole
 * @Date 2020/4/19 19:53
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    CategoryMapper categoryMapper;

    private List<Category> cates = new ArrayList<>();
    /**
     * TODO 查询所有
     * @return
     */
    @Override
    public List<Object> findAll() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("id");
        List<Category> categories = categoryMapper.selectList(null);
        if (CollectionUtils.isEmpty(categories)) {
            return null;
        }
        ArrayList<Tree> trees = new ArrayList<>();
        categories.forEach(x -> {
            trees.add(new Tree(x.getParentId(), x, x.getId()));
        });
        List<Object> objects = TreeUtils.distinct(trees);
        return objects;
    }

    /**
     * TODO 添加
     * @param category
     * @return
     */
    @Override
    public Result addCategory(Category category) {
        int insert = categoryMapper.insert(category);
        if (insert > 0) {
            return Result.SUCCESS();
        }
        return Result.FAIL();
    }

    /**
     * TODO 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        int i = categoryMapper.deleteById(id);
        if (i > 0) {
            return Result.SUCCESS();
        }
        return Result.FAIL();
    }

    /**
     * TODO 修改
     * @param category
     * @return
     */
    @Override
    public Result updateById(Category category) {
        int i = categoryMapper.updateById(category);
        if (i > 0) {
            return Result.SUCCESS();
        }
        return Result.FAIL();
    }

    /**
     * TODO 根据id查
     * @param id
     * @return
     */
    @Override
    public Result findById(Integer id) {
        Category category = categoryMapper.selectById(id);
        if (ObjectUtils.isEmpty(category)){
            return Result.FAIL();
        }
        return new Result(ResultCode.SUCCESS, category);
    }

    /**
     * TODO 根据商品id查询商品类型
     * @param id
     * @return
     */
    @Override
    public Result findByProdId(Integer id) {
        ArrayList<Category> all = new ArrayList<>();
        // 查询所商品的类型列表
        List<Category> categories = categoryMapper.findByProdId(id);
        // 循环查找类型的父类型
        categories.parallelStream().forEach(x -> {
            finParentList(x);
            all.add(x);
            // 递归查找，并将查找结构添加到all集合中
            all.addAll(finParentList(categories.get(0)));
        });
        List<Category> collect = all.stream().distinct().sorted(Comparator.comparing(Category::getId)).collect(Collectors.toList());
        System.out.println(all.size());
        return new Result(ResultCode.SUCCESS, collect);
    }

    /**
     * TODO 根据商品id给商品添加类型
     * @param id 商品id
     * @param ids 类型的id（不能是父类型）
     * @return
     */
    @Override
    public Result addByProdId(Integer id, List<Integer> ids) {
        ids.forEach(item -> {
            categoryMapper.addByProdId(id, item);
        });
        return Result.SUCCESS();
    }

    /**
     * TODO 根据商品id删除商品类型id
     * @param id
     * @return
     */
    @Override
    public Result deleteByProdId(Integer id) {
        int i = categoryMapper.deleteByProdId(id);
        if (i < 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 修改商品类型，先删除，后修改
     * @param id
     * @param ids 类型集合
     * @return
     */
    @Override
    public Result updateByProdId(Integer id, List<Integer> ids) {
        categoryMapper.deleteByProdId(id);
        ids.forEach(item -> {
            categoryMapper.addByProdId(id, item);
        });
        return Result.SUCCESS();
    }

    /**
     * TODO 递归取类型的父类型
     * @param category
     * @return
     */
    private List<Category> finParentList(Category category){
        if (category.getParentId() != 0) {
            Category c = categoryMapper.selectById(category.getParentId());
            cates.add(c);
            finParentList(c);
        }
        return cates;
    }



}
